package com.google.code.retrobox.editor.commons.dialog;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

import javax.swing.*;

import com.google.code.retrobox.editor.maps.MapEditor.Pos;


public class AddRowDialog extends JDialog implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<ActionListener> listeners = new LinkedList<ActionListener>();
	
	// Components
	private JComboBox posBox;
	
	private JSpinner rowSpinner;
	
	private JButton		okButton = new JButton("OK!"),
						cancelButton = new JButton("Cancel");
	
	// Data
	private Pos pos = Pos.NORTH;
	
	
	public AddRowDialog(JFrame parent) {
		super(parent, false);
		init();
	}
	
	public void init() {
		setLayout(new BorderLayout());
		
		
		okButton.addActionListener(this);
		cancelButton.addActionListener(this);
		
		JPanel tmp = new JPanel(new BorderLayout());
		tmp.add(okButton, BorderLayout.WEST);
		tmp.add(cancelButton, BorderLayout.EAST);
		
		add(tmp, BorderLayout.SOUTH);
		
		String str[] = {"North", "South", "East", "West"};
		posBox = new JComboBox(str);
		posBox.addActionListener(this);
		
		SpinnerNumberModel mod = new SpinnerNumberModel(1, 1, Integer.MAX_VALUE, 1);
		rowSpinner = new JSpinner(mod);
		
		JPanel selPanel = new JPanel(new BorderLayout());
		JPanel boxPanel = new JPanel(new BorderLayout());
		JPanel spinPanel = new JPanel(new BorderLayout());
		
		boxPanel.add(posBox);
		boxPanel.setBorder(BorderFactory.createTitledBorder("Where to add rows"));
		
		spinPanel.add(rowSpinner);
		spinPanel.setBorder(BorderFactory.createTitledBorder("Number of rows to add"));
		
		selPanel.add(boxPanel, BorderLayout.NORTH);
		selPanel.add(spinPanel, BorderLayout.SOUTH);
		
		add(selPanel, BorderLayout.NORTH);
		
		setTitle("Add rows");
//		pack();
		setSize(new Dimension(300, 200));
		setLocationRelativeTo(null);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setVisible(true);
	}
	
	public Pos getPos() {
		return pos;
	}
	
	public int gerRowNum() {
		return (Integer)rowSpinner.getValue();
	}
	
	public void addActionListener(ActionListener listener) {
		listeners.add(listener);
	}

	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == okButton) {
			for (ActionListener element: listeners) {
				element.actionPerformed(new ActionEvent(this, 1, "Data selected"));
			}
			this.dispose();
			
		} else if (arg0.getSource() == cancelButton) {
			this.dispose();
		} else if (arg0.getSource() == posBox) {
			JComboBox cb = (JComboBox)arg0.getSource();
	        String posName = (String)cb.getSelectedItem();
	        
	        if (posName.compareTo("North") == 0) {
	        	pos = Pos.NORTH;
	        } else if (posName.compareTo("South") == 0) {
	        	pos = Pos.SOUTH;
	        } else if (posName.compareTo("East") == 0) {
	        	pos = Pos.EAST;
	        } else if (posName.compareTo("West") == 0) {
	        	pos = Pos.WEST;
	        }
		}
	}
}












